package com.link.base.tencent.ocr.controller;

import com.link.base.tencent.ocr.model.OcrImg;
import com.link.base.tencent.ocr.service.OcrService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 接入腾讯云文字识别
 *
 * @author ouyangzhenqiang
 * @date 2018/12/29
 */
@Controller
@RequestMapping("link/ocr")
public class OcrController {

    @Resource
    public OcrService ocrService;

    /**
     * 名片识别
     * 基于图片base64编码创建服务器图片，并调用腾讯云名片识别接口，识别名片获取名片信息
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     */
    @RequestMapping(value = "/businessCard", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> businessCard(@RequestBody OcrImg record, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        LCLogger.info().withMessageKey("businessCard").withMessage("OCR-名片识别..." + record.getUrl()).flush();

        try {
            // 调用腾讯名片识别接口
            String cardInfo = ocrService.businessCard(record);
            result.put("success", true);
            result.put("result", cardInfo);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 身份证识别
     * 基于图片base64编码创建服务器图片，并调用腾讯云身份证识别接口，识别身份证信息
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     */
    @RequestMapping(value = "/idCard", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> idCard(@RequestBody OcrImg record, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        LCLogger.info().withMessageKey("businessCard").withMessage("OCR-身份证识别..." + record.getUrl()).flush();

        try {
            // 调用腾讯身份证识别接口
            String idCardInfo = ocrService.idCard(record);
            result.put("success", true);
            result.put("result", idCardInfo);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
